package com.aode.music.view.fragment.mefragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aode.common.bean.response.Music;
import com.aode.downloadmusic.adapter.DownloadMusicAdapter;
import com.aode.music.R;
import com.aode.music.adapter.me.DownLoadAdapter;
import com.aode.music.databinding.FragmentDownloadBinding;
import com.aode.music.viewmodel.DownloadViewModel;

import java.util.ArrayList;
import java.util.List;

public class DownLoadFragment extends Fragment {
    private FragmentDownloadBinding downloadBinding;
    private DownLoadAdapter downLoadAdapter;
    private DownloadViewModel downloadViewModel;
    private List<Music> musicList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        downloadBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_download,container,false);
        downloadViewModel = ViewModelProviders.of(this).get(DownloadViewModel.class);
        initData();
        return downloadBinding.getRoot();
    }

   public void initData() {
        downloadViewModel.getMusic();
        intListener();
    }

    private void intListener() {
        downloadViewModel.getMusicLiveData().observe(getViewLifecycleOwner(), music -> {
            musicList = music;
            if(musicList!=null&&!musicList.isEmpty()&&musicList.size()>0){
                initRecycler((ArrayList<Music>)musicList);
            }
        });
    }

    private void initRecycler(ArrayList<Music> musicList) {
        downLoadAdapter = new DownLoadAdapter(getContext(),musicList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext(),LinearLayoutManager.VERTICAL,false);
        downloadBinding.downloadRecycler.setLayoutManager(linearLayoutManager);
        downloadBinding.downloadRecycler.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL));
        downloadBinding.downloadRecycler.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.HORIZONTAL));
        downloadBinding.downloadRecycler.setAdapter(downLoadAdapter);
    }
}
